<?php
namespace Wap\Model;
use Think\Model;

class CustomerAddrModel extends Model
{
    protected $tableName = "customer_addr";
    //protected $patchValidate = true;
    protected $_validate = array(
        array( "username", "require", "用户名不能为空", 1 ),
        array( "username", "/^[\x{4E00}-\x{9FA5}]+$/u", "用户名需为中文", 1, "regex" ),
        array( "username", "len", "用户名为2-20位", 1, "callback", 1, array( array( "min" => 2, "max" => 20 ) ) ),
        array( "telephone", "/(^1\d{10}$)/", "手机号码格式不正确", 1, "regex"  ),
        array( "province", "require", 1, '省不能为空' ),
        array( "city", "require", 1, '市不能为空' ),
        array( "county", "require", 1, '区不能为空' ),
        array( "address", "require", "地址不能为空", 1 ),
        // array( "address", "/^[\x{4E00}-\x{9FA5}a-zA-Z0-9]+$/u", "地址信息不合法", 1, "regex" ),
        array( "metier", "require", "职业不能为空" ),
        array( "customer_id", "require", "用户不在线", 1 )
    );

    public function addAddress( $data )
    {
        $result = array( "err" => 400, "msg" => "添加地址出错" );
        $added = $this->add( $data );
        if( $added === false ){
            return $result;
        }else if( $added === 0 ){
            $result['err'] = 403;
            $result['msg'] = "添加地址失败";
        }else if( $added > 0 ){
            $result['err'] = 200;
            $result['msg'] = "添加地址成功";
        }

        return $result;
    }

    public function updateAddress( $addr_id, $data )
    {
        $result = array( "err" => 400, "msg" => "更新地址出错" );
        $condition['id'] = $addr_id;
        $updated = $this->where( $condition )->save( $data );
        if( $updated === false ){
            return $result;
        }else if( $updated === 0 ){
            $result['err'] = 304;
            $result['msg'] = "没有数据更新";
        }else if( $updated === 1 ){
            $result['err'] = 200;
            $result['msg'] = "地址更新成功";
        }

        return $result;
    }

    public function len( $str, $condition )
    {
        $minRequire = true;
        $maxRequire = true;
        trace( $condition, "len", "INFO" );
        trace( $str, "len str", "INFO" );
        if( isset( $condition['min'] ) ){
            $minRequire = mb_strlen($str, 'utf-8') >= $condition['min'] ? true : false; 
        }
        if( isset( $condition['max'] ) ){
            $maxRequire = mb_strlen($str, 'utf-8') <= $condition['max'] ? true : false;
        }
        return ($minRequire && $maxRequire);
    }

    private function requireExistProvince( $province )
    {
        return false;
        $condition['id'] = $province;
        $founed = M( 'region' )->where( $condition )->find();
        if( $founed ){
             return true;
        }else{
            return false;
        }
    }

    private function requireExistCity( $city, $_data )
    {
        $condition['parentid'] = $_data['province'];
        $condition['id'] = $city;
        $founed = M( 'region' )->where( $condition )->find();
        if( $founed ){
             return true;
        }else{
            return false;
        }
    }

    private function requireExistCounty( $county, $_data )
    {
        //$condition['province'] = $_data['province'];
        $condition['parentid'] = $_data['city'];
        $condition['id'] = $_data['county'];
        $founed = M( 'region' )->where( $condition )->find();
        if( $founed ){
             return true;
        }else{
            return false;
        }
    }
}
?>
